<template>
  <div>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <list-table
        ref="table"
        :request="loadData"
        :statistics-config="{ use: true, statistics}"
        :export-config="{ use: true,select: '物资招标计划清单导出选中',all: '物资招标计划清单导出全部'}"
        :columns="columns"
        row-key="id"
      >
        <template
          slot="prefix"
          slot-scope="ref"
        >
          <a-button
            v-btnPermission="'mm_004_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>
        </template>
        <span
          slot="index"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template
          slot="auditStatusStr"
          slot-scope="text, record, index"
        >
          <list-status
            :status="record.auditStatus"
            :text="text"
          />
        </template>
        <div
          slot="operation"
          slot-scope="text, record, index"
          class="pub-table-list-action"
        >
          <span @click="handleEdit(record, 3)">查看</span>
          <template v-if="record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED'">
            <span @click="() => handleEdit(record, 2)">编辑</span>
            <span @click="() => handleDel(record.id)">删除</span>
          </template>
          <approval-button
            :id="record.id"
            :status="record.auditStatus"
            v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' "
          />
        </div>
      </list-table>
    </a-card>
  </div>
</template>
<script>
import { planList, planListDel, findListCount,print } from '@/api/material/procurement/plan'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'

export default {
	name:'a'+Date.now(),
        data: function() {
        return {
            searchArr: [
                // 搜索框
                {
                    name: '所属分公司',
                    valueKey: 'branchCompanyId',
                    type: SelectSubCompany
                },
                // {
                //     name: '项目名称',
                //     type: SelectProject,
                //     valueKey: 'projectId'
                // },
                {
                    name: '招标计划编码',
                    value: 'tenderCode',
                    type: 'input'
                },
                {
                    name: '工程名称',
                    value: 'wbsId',
                    type: 'select',
                    data: [],
                    request: () => selectOption({ type: 18 })
                },
                {
                    name: '起始时间',
                    value: 'start',
                    type: 'dateRange'
                },
                {
                    name: '截止时间',
                    value: 'end',
                    type: 'dateRange'
                },
                {
                    name: '创建人',
                    value: 'creatorName',
                    type: 'input'
                },
                {
                    name: '创建时间',
                    value: 'createTime',
                    type: 'timeRangeFormat'
                },
                {
                    name: '状态',
                    value: 'auditStatus',
                    type: 'select',
                    data: this.STATUS
                }
            ],
            columns: [
                {
                    title: '序号',
                    dataIndex: 'index',
                    scopedSlots: { customRender: 'index' },
                    width: 70
                }, {
                    title: '所属分公司',
                    dataIndex: 'branchCompany',
                    width: 150
                },
                {
                    title: '项目名称',
                    dataIndex: 'project',
                    width: 150
                },
                {
                    title: '物资招标计划编码',
                    dataIndex: 'tenderCode',
                    width: 160
                },
                {
                    title: '工程名称',
                    dataIndex: 'wbsName',
                    width: 140
                },
                {
                    title: '起始时间',
                    dataIndex: 'startDate',
                    width: 160
                },
                {
                    title: '截止时间',
                    dataIndex: 'endDate',
                    width: 160
                },
                {
                    title: '创建人',
                    dataIndex: 'creatorName',
                    width: 150
                },
                {
                    title: '创建时间',
                    dataIndex: 'createTime',
                    width: 160
                },
                {
                    title: '状态',
                    dataIndex: 'auditStatusStr',
                    width: this.STATUS_WIDTH,
                    scopedSlots: { customRender: 'auditStatusStr' }
                },
                {
                    title: '操作',
                    dataIndex: 'operation',
                    scopedSlots: { customRender: 'operation' },
                    width: 160,
                    fixed: 'right'
                }
            ],
            queryParam: {},

            loadData: async params => {
                let searchData = Object.assign(params, this.queryParam)
                if (searchData.start && searchData.start.length) {
                    searchData.startDateStart = this.formatDate(searchData.start[0], true)
                    searchData.startDateEnd = this.formatDate(searchData.start[1], true)
                }
                if (searchData.end && searchData.end.length) {
                    searchData.endDateStart = this.formatDate(searchData.end[0])
                    searchData.endDateEnd = this.formatDate(searchData.end[1])
                }
                delete searchData.start
                delete searchData.end
                delete searchData.createTime
                findListCount(searchData).then(res => {if (res.code === this.SUCCESS_CODE) this.statistics = res.data})
                return planList(searchData)
            },
            statistics: {},
            beforePrintFn: async () => {
                let res = await print({ id: this.form.id })
                // res.data = res.data.replaceAll('\n', '\\')
                return res.data.content
            }
        }
    },
    mounted: function() {
    },
    methods: {
        search(val) {
            this.queryParam = val
            this.$refs.table.getData(null, true)
        },
        handleDel(id) {
            let params = id ? [id] : _this.selectId
            this.delList(planListDel, () => {
                this.search()
            }, { ids: params })

        },
        handleEdit(record, state) {
            this.$router.push({
                path: '/material/procurement/planAdd',
                query: {
                    id: record && record.id || '',
                    state: state
                }
            })
        }
    }
}
</script>
